﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace QunYing.Web.market
{
    public partial class orderone : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Repeater rp = ((Repeater)Master.FindControl("NavRepeater"));
                rp.DataSource = NavData();
                rp.DataBind();
            }
        }
        /// <summary>
        /// 快捷导航数据
        /// </summary>
        private DataTable NavData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("url");
            dt.Rows.Add("购物车结算", "");
            return dt;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            DataTable dt = (DataTable)Session["ShopCart"];
            GridView1.DataSource = dt;
            GridView1.DataBind();
            if (dt != null)
            {
                decimal totalPrice = 0;
                int useScore = 0;
                int getScore = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    totalPrice += Convert.ToDecimal(dt.Rows[i]["单价"]) * Convert.ToInt32(dt.Rows[i]["数量"]);
                    useScore += Convert.ToInt32(dt.Rows[i]["消耗积分"]) * Convert.ToInt32(dt.Rows[i]["数量"]);
                    getScore += Convert.ToInt32(dt.Rows[i]["赠送积分"]) * Convert.ToInt32(dt.Rows[i]["数量"]);
                }
                lblTotalPrice.Text = totalPrice.ToString("f2");
                lblUseScore.Text = useScore.ToString();
                lblGetScore.Text = getScore.ToString();
            }

        }

        protected void btnBack_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/market.aspx");
        }

        protected void btnGo_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["ShopCart"] == null)
            {
                MessageBox.Show(this, "没有选购商品");
                return;
            }
            if (((DataTable)Session["ShopCart"]).Rows.Count > 0)
            {
                Response.Redirect("~/ordertwo.aspx");
            }
            else
            {
                MessageBox.Show(this, "没有选购商品");
            }

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            int index = Convert.ToInt32((sender as LinkButton).CommandArgument);
            string id = this.GridView1.DataKeys[index].Values[0].ToString();//商品ID
            string type = this.GridView1.DataKeys[index].Values[1].ToString();//类型
            ShopCart.DelGoods(id, type);
            BindData();
            Label lblNum = ((Label)Master.FindControl("lblNum"));
            DataTable dt = (DataTable)Session["ShopCart"];
            string js = "$('#"+lblNum.ClientID+"').html('"+ dt.Rows.Count +"');";
            MessageBox.ResponseScript(UpdatePanel1,js);

        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            int index = Convert.ToInt32((sender as ImageButton).CommandArgument);
            string id = this.GridView1.DataKeys[index].Values[0].ToString();//商品ID
            string type = this.GridView1.DataKeys[index].Values[1].ToString();//类型
            TextBox txtNum = (TextBox)GridView1.Rows[index].FindControl("txtNum");
            try
            {
                int i = Convert.ToInt32(txtNum.Text.ToString()) + 1;
                if (i > 0)
                {
                    ShopCart.UpdateGoods(id, i, type);
                    BindData();
                }
            }
            catch (Exception)
            {
                MessageBox.Show(UpdatePanel1, "请输入正确数量(大于0)!");
            }
        }

        protected void btnSub_Click(object sender, ImageClickEventArgs e)
        {
            int index = Convert.ToInt32((sender as ImageButton).CommandArgument);
            string id = this.GridView1.DataKeys[index].Values[0].ToString();//商品ID
            string type = this.GridView1.DataKeys[index].Values[1].ToString();//类型
            TextBox txtNum = (TextBox)GridView1.Rows[index].FindControl("txtNum");
            try
            {
                int i = Convert.ToInt32(txtNum.Text.ToString())-1;
                if (i > 0)
                {
                    ShopCart.UpdateGoods(id, i, type);
                    BindData();
                }

            }
            catch (Exception)
            {
                MessageBox.Show(UpdatePanel1, "请输入正确数量(大于0)!");
            }

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                TextBox txtNum = (TextBox)e.Row.FindControl("txtNum");
                txtNum.Text = drv["数量"].ToString();
            }
        }

        protected void txtNum_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            GridViewRow drv = (GridViewRow)t.NamingContainer;
            int index = drv.RowIndex;
            string id = this.GridView1.DataKeys[index].Values[0].ToString();//商品ID
            string type = this.GridView1.DataKeys[index].Values[1].ToString();//类型
            TextBox txtNum = (TextBox)GridView1.Rows[index].FindControl("txtNum");
            try
            {
                int i = Convert.ToInt32(txtNum.Text.ToString());
                if (i > 0)
                {
                    ShopCart.UpdateGoods(id, i, type);
                    BindData();
                }
                else
                {
                    MessageBox.Show(UpdatePanel1, "请输入正确数量(大于0)!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show(UpdatePanel1, "请输入正确数量(大于0)!");
            }
        }
    }
}
